﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace OPC.DA.Redis.Wrapper
{
    public class Xml
    {
        static public string GetAttribute(XmlNode node, string attr, string def = "")
        {
            try
            {
                return node.Attributes[attr].Value;
            }
            catch
            {
                return def;
            }
        }

        //将XML节点按实体T的属性进行转换赋值，要求T的属性与节点属性名称一致
        static public IList<T> Xml2Entities<T>(XmlDocument xmldoc, string xpath) where T : new()
        {
            IList<T> dcl = new List<T>();
            try
            {
                // 获得配置文件的全路径　　  
                XmlNodeList nodelist = xmldoc.SelectNodes(xpath);
                IList<string> lsa = null;
                foreach (XmlNode node in nodelist)
                {
                    T entity = new T();

                    //取ServiceInfo的所有属性名称
                    if (lsa == null)
                    {
                        lsa = new List<string>();
                        IList<System.Reflection.PropertyInfo> sia = entity.GetType().GetProperties();
                        foreach (System.Reflection.PropertyInfo pi in sia)
                        {
                            lsa.Add(pi.Name);
                        }
                    }
                    foreach (XmlAttribute xa in node.Attributes)
                    {
                        //xa.Name如果与ServiceInfo属性匹配上，则设置其值。只能处理简单类型
                        if (lsa.Contains(xa.Name) == true)
                        {
                            var item = entity.GetType().GetProperty(xa.Name);
                            if (item.PropertyType == typeof(System.TimeSpan))
                            {
                                item.SetValue(entity, TimeSpan.Parse(xa.Value), null);
                            }
                            else
                            {
                                item.SetValue(entity, Convert.ChangeType(xa.Value, item.PropertyType), null);
                            }
                        }
                    }
                    dcl.Add(entity);
                }
            }
            catch (Exception e)
            {
            }
            return dcl;
        }

        static public T Xml2Entity<T>(XmlDocument xmldoc, string xpath) where T : new()
        {
            T entity = new T();
            try
            {
                // 获得配置文件的全路径　　  
                XmlNode node = xmldoc.SelectNodes(xpath).Item(0);
                IList<string> lsa = null;
                //取所有属性名称
                if (lsa == null)
                {
                    lsa = new List<string>();
                    IList<System.Reflection.PropertyInfo> sia = entity.GetType().GetProperties();
                    foreach (System.Reflection.PropertyInfo pi in sia)
                    {
                        lsa.Add(pi.Name);
                    }
                }
                foreach (XmlAttribute xa in node.Attributes)
                {
                    //xa.Name如果属性匹配上，则设置其值。只能处理简单类型
                    if (lsa.Contains(xa.Name) == true)
                    {
                        var item = entity.GetType().GetProperty(xa.Name);
                        if (item.PropertyType == typeof(System.TimeSpan))
                        {
                            item.SetValue(entity, TimeSpan.Parse(xa.Value), null);
                        }
                        else
                        {
                            item.SetValue(entity, Convert.ChangeType(xa.Value, item.PropertyType), null);
                        }
                    }
                }
            }
            catch
            {
            }

            return entity;
        }
        static public Dictionary<string,string>  Xml2Entity(XmlDocument xmldoc, string xpath)
        {
            Dictionary<string, string> entity = new Dictionary<string, string>();
            try
            {
                // 获得配置文件的全路径　　  
                XmlNode node = xmldoc.SelectNodes(xpath).Item(0);
                foreach (XmlAttribute xa in node.Attributes)
                {
                    entity.Add(xa.Name, xa.Value);
                }
            }
            catch
            {
            }

            return entity;
        }

        static public bool Xml2Entity<T>(T entity, XmlNodeList nodelist)
        {
            bool bRet = true;
            try
            {
                IList<string> lsa = null;
                foreach (XmlNode node in nodelist)
                {

                    //取所有属性名称
                    if (lsa == null)
                    {
                        lsa = new List<string>();
                        IList<System.Reflection.PropertyInfo> sia = entity.GetType().GetProperties();
                        foreach (System.Reflection.PropertyInfo pi in sia)
                        {
                            lsa.Add(pi.Name);
                        }
                    }
                    foreach (XmlAttribute xa in node.Attributes)
                    {
                        //xa.Name如果属性匹配上，则设置其值。只能处理简单类型
                        if (lsa.Contains(xa.Name) == true)
                        {
                            var item = entity.GetType().GetProperty(xa.Name);
                            if (item.PropertyType == typeof(System.TimeSpan))
                            {
                                item.SetValue(entity, TimeSpan.Parse(xa.Value), null);
                            }
                            else
                            {
                                item.SetValue(entity, Convert.ChangeType(xa.Value, item.PropertyType), null);
                                //entity.GetType().GetProperty(xa.Name).SetValue(entity, Convert.ChangeType(xa.Value, null);
                                //item.SetValue(entity, xa.Value, null);
                            }
                        }
                    }
                }
            }
            catch
            {
                bRet = false;
            }
            return bRet;
        }
    }
}
